import { DataProvider } from "../../../../fl/data/DataProvider";
import { GButton } from "../../../../com/mui/controls/GButton";
import { SmeltTypeData } from "./data/SmeltTypeData";
import { GTileList } from "../../../../com/mui/controls/GTileList";
import { ObjCreate } from "../common/ObjCreate";
import { SmeltPlanListCellRenderer } from "./cell/SmeltPlanListCellRenderer";
import { ScrollBarDirection } from "../../../../fl/controls/ScrollBarDirection";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { Global } from "../../../../com/gengine/global/Global";
import { Cache } from "../../cache/Cache";
import { SmeltPlanData } from "./data/SmeltPlanData";
import { _buttonRowHeight } from "../../../../this/_buttonRowHeight";
import { Vector } from "../../../../__AS3__/vec/Vector";
type int = number;
//class SmeltPlanListPanel
    
    export  class SmeltPlanListPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._planListData = [];
            this._plansData = new DataProvider();
            this._buttons = new Vector.<GButton>();
            this._typeDatas = new Vector.<SmeltTypeData>();
            this.createChildren();
            this.addListeners();
            return;
        }

        public get planTileList(): GTileList
        {
            return this._planTileList;
        }

        protected createChildren(): void
        {
            this._planTileList = ObjCreate.createTileList(0, 0, 185, 100, this);
            this._planTileList.columnWidth = 160;
            this._planTileList.rowHeight = 47;
            this._planTileList.setStyle("cellRenderer", SmeltPlanListCellRenderer);
            this._planTileList.direction = ScrollBarDirection.VERTICAL;
            return;
        }

        protected addListeners(): void
        {
            this._planTileList.addEventListener(flash.events.Event.CHANGE, this.onSelectedPlanChange);
            return;
        }

        public set planListData(arg1: Array<any>)
        {
            var loc1=null;
            var loc3=null;
            this._planListData = arg1;
            this.clearButtons();
            var loc2=0;
            while (loc2 < arg1.length) 
            {
                loc1 = arg1[loc2] as SmeltTypeData;
                (loc3 = ObjCreate.createGButton("", 3, 0, 185, 26, this)).label = loc1.smeltTypeName;
                loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
                this._buttons.push(loc3);
                this._typeDatas.push(loc1);
                ++loc2;
            }
            this._planTileList.height = this._totalHeight - this._buttonRowHeight * arg1.length;
            this.updateListData();
            this.onResize();
            return;
        }

        public get planListData(): Array<any>
        {
            return this._planListData;
        }

        public set selectedIndex(arg1: int)
        {
            if (this._selectedIndex != arg1) 
            {
                this._selectedIndex = arg1;
                this.updateListData();
                this.onResize();
            }
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.selectedIndex = this._buttons.indexOf(arg1.target);
            return;
        }

        /* internal  */onSelectedPlanChange(arg1: Object /* flash.events.Event */): void
        {
            this.updateSelectedPlan();
            return;
        }

        public updateListData(): void
        {
            var loc2=null;
            Dispatcher.dispatchEvent(new DataEvent(EventName.SmeltCountPlanListNum, null));
            var loc1=this._planTileList.selectedIndex;
            this._plansData.removeAll();
            if (this._typeDatas.length > this._selectedIndex) 
            {
                var loc3=0;
                var loc4=this._typeDatas[this._selectedIndex].smeltPlanList;
                for(loc2 of loc4) 
                {
                    if (!loc2 && !Global.isDebugModle) 
                    {
                        continue;
                    }
                    else if (loc2.showLevel > Cache.instance.role.roleInfo.level) 
                    {
                        continue;
                    }
                    else if (loc2.minLevel > Cache.instance.role.activeWorldLevel || loc2.maxLevel < Cache.instance.role.activeWorldLevel) 
                    {
                        continue;
                    }
                    if (Cache.instance.smelt.isShowCanSmeltOnly && loc2.canSmeltNum > 0) 
                    {
                        this._plansData.addItem(loc2);
                        continue;
                    }
                    if (Cache.instance.smelt.isShowCanSmeltOnly) 
                    {
                        continue;
                    }
                    this._plansData.addItem(loc2);
                }
                this._planTileList.dataProvider = this._plansData;
                if (this._plansData.length > 0) 
                {
                    if (loc1 < 0 || loc1 > (this._plansData.length - 1)) 
                    {
                        loc1 = 0;
                    }
                    this._planTileList.selectedItem = this._plansData.getItemAt(loc1);
                }
                else 
                {
                    this._planTileList.selectedItem = null;
                }
            }
            else 
            {
                this._planTileList.dataProvider = this._plansData;
            }
            this.updateSelectedPlan();
            return;
        }

        public setSelectedIndexByType(arg1: int): void
        {
            var loc1=0;
            loc1 = 0;
            while (loc1 < this._typeDatas.length) 
            {
                if (this._typeDatas[loc1].smeltType == arg1) 
                {
                    break;
                }
                ++loc1;
            }
            this.selectedIndex = loc1;
            return;
        }

        public setSelectedPlanByPlanCode(arg1: int, arg2: Boolean=false): void
        {
            var loc2=null;
            var loc1=this._planTileList.dataProvider;
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                if ((loc2 = loc1.getItemAt(loc3) as SmeltPlanData).smeltPlanCode == arg1) 
                {
                    this._planTileList.selectedItem = loc2;
                    if (arg2) 
                    {
                        this._intervalId = flash.utils.setInterval(this.showGuide, 600);
                    }
                    break;
                }
                ++loc3;
            }
            this.updateSelectedPlan();
            return;
        }

        /* internal  */showGuide(): void
        {
            flash.utils.clearInterval(this._intervalId);
            var loc1=this._planTileList.selectedItem as SmeltPlanData;
            var loc2=this._planTileList.itemToCellRenderer(loc1) as SmeltPlanListCellRenderer;
            if (loc2) 
            {
                loc2.addGuide();
            }
            return;
        }

        public get currentShowedPlanListData(): Array<any>
        {
            return this._typeDatas[this._selectedIndex].smeltPlanList;
        }

        public updateSelectedPlan(): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.SmeltSelectedPlanChange, this._planTileList.selectedItem));
            return;
        }

        /* internal  */clearButtons(): void
        {
            var loc1=(this._buttons.length - 1);
            while (loc1 >= 0) 
            {
                if (this._buttons[loc1].parent) 
                {
                    this._buttons[loc1].parent.removeChild(this._buttons[loc1]);
                }
                this._buttons.pop();
                this._typeDatas.pop();
                --loc1;
            }
            return;
        }

        /* internal  */onResize(): void
        {
            var loc1=0;
            while (loc1 < this._buttons.length) 
            {
                this._buttons[loc1].y = loc1 <= this._selectedIndex ? this._buttonRowHeight * loc1 : _buttonRowHeight * loc1 + this._planTileList.height;
                ++loc1;
            }
            this._planTileList.y = this._buttonRowHeight * (this._selectedIndex + 1);
            return;
        }

        /* internal */ /* var */_buttonRowHeight: int=32;

        /* internal */ /* var */_totalHeight: int=352;

        /* internal */ /* var */_planListData: Array<any>;

        /* internal */ /* var */_plansData: DataProvider;

        /* internal */ /* var */_selectedIndex: int=0;

        /* internal */ /* var */_buttons: Vector.<GButton>;

        /* internal */ /* var */_typeDatas: Vector.<SmeltTypeData>;

        /* internal */ /* var */_planTileList: GTileList;

        /* internal */ /* var */_intervalId: uint;
    }
